﻿using Atlanchim.Crm.Mui.Commands;
using Atlanchim.Crm.Mui.Helpers;
using FirstFloor.ModernUI.Presentation;
using FirstFloor.ModernUI.Windows.Controls;
using Lmondeil.Crm.EF;
using System;
using System.Collections.Generic;
using System.Data.Entity.Validation;
using System.Linq;
using System.Text;

namespace Atlanchim.Crm.Mui.Pages.Societes
{
    public class PageViewSocieteModel : AbstractPageViewModel
    {
        /// <summary>
        /// Société de référence
        /// </summary>
        public Societe Societe { get; set; }

        /// <summary>
        /// Url d'accès à la page d'édition
        /// </summary>
        public override string EditUrl { get; set; }

        /// <summary>
        /// Informations sur les contacts
        /// </summary>
        public int ContactsCount { get; set; }
        public string ContactsListUrl { get; private set; }

        /// <summary>
        /// Informations sur les actions
        /// </summary>
        public int ActionsCount { get; set; }
        public string ActionsListUrl { get; private set; }

        /// <summary>
        /// Informations sur les adresses
        /// </summary>
        public string AdressesListUrl { get; private set; }

        /// <summary>
        /// Informations sur les documents
        /// </summary>
        public string DocumentsListUrl { get; private set; }


        /// <summary>
        /// Chargement
        /// </summary>
        public override void Load()
        {
            // Lecture du fragment
            int idSociete = 0;
            int.TryParse(this.DicFragments["IdSociete"], out idSociete);

            // Chargement
            using (CrmContext context = new CrmContext())
            {
                // Société
                this.Societe = context
                                .Societes
                                .Where(societe => societe.Id == idSociete)
                                .FirstOrDefault();
                EditUrl = string.Format("/Pages/Societes/PageEditSociete.xaml#IdSociete={0}", idSociete);

                // Contacts
                IEnumerable<int> contactIds = context
                                .Contacts
                                .Where(contact => contact.IdSociete == idSociete)
                                .Select(c => c.Id);
                this.ContactsCount = contactIds.Count();
                this.ContactsListUrl = string.Format("/Pages/Contacts/PageListContacts.xaml#IdSociete={0}", idSociete);

                // Actions
                this.ActionsCount = context
                                .Actions
                                .Where(action => contactIds.Contains(action.Id))
                                .Count();
                this.ActionsListUrl = string.Format("/Pages/Actions/PageListAllActions.xaml#IdSociete={0}", idSociete);

                // Adresses
                this.AdressesListUrl = string.Format("/Pages/Adresses/PageListAdresses.xaml#IdSociete={0}", idSociete);

                // Documents
                this.DocumentsListUrl = string.Format("/Pages/Documents/PageListDocuments.xaml#IdSociete={0}", idSociete);

                // TabLinks
                IEnumerable<Link> bufferLinks = new Link[]
                {
                    new Link(){DisplayName="Contacts", Source = new Uri(this.ContactsListUrl, UriKind.RelativeOrAbsolute)},
                    new Link(){DisplayName="Actions", Source = new Uri(this.ActionsListUrl, UriKind.RelativeOrAbsolute)},
                    new Link(){DisplayName="Adresses", Source = new Uri(this.AdressesListUrl, UriKind.RelativeOrAbsolute)},
                    new Link(){DisplayName="Devis & Contrats", Source = new Uri(this.DocumentsListUrl, UriKind.RelativeOrAbsolute)}
                };
                this.TabLinks = new LinkCollection(bufferLinks);
            }

            // Notification
            OnPropertyChanged("Societe");
        }


        /// <summary>
        /// Suppression
        /// </summary>
        protected override void OnDeleteCurrentEntity()
        {
            CrmCrudHelper.DeleteEntityNewContext(this.Societe);
        }
    }
}
